package com.xujiakai.car.maintain.application.service.impl;

import com.github.pagehelper.PageHelper;
import com.xujiakai.car.maintain.application.service.IBaseDataManageService;
import com.xujiakai.car.maintain.domain.dto.*;
import com.xujiakai.car.maintain.domain.entity.*;
import com.xujiakai.car.maintain.infrastructure.mapper.DaoFactory;
import com.xujiakai.car.maintain.infrastructure.utils.BeanUtil;
import com.xujiakai.car.maintain.infrastructure.utils.PagedResult;
import com.xujiakai.car.maintain.userinterface.views.CustomerView;
import com.xujiakai.car.maintain.userinterface.views.MainItemView;
import com.xujiakai.car.maintain.userinterface.views.UserSector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 基础数据的服务
 *
 * @author desenyang tz
 */
@Service
public class BaseDataManageService implements IBaseDataManageService {

    public static final Logger logger = LoggerFactory.getLogger(BaseDataManageService.class);

    @Resource
    private DaoFactory daoFactory;

    @Resource
    Customer customerDTO;

    @Resource
    Permission permissionDTO;

    @Resource
    Comment commentDTO;

    @Resource
    Mainitem mainitemDTO;

    @Resource
    Projcategory projcategoryDTO;

    @Resource
    Supplier supplierDTO;

    @Resource
    Part partDTO;

    @Resource
    Warehouse warehouseDTO;

    @Resource
    Partcategory partcategoryDTO;
    /**
     * 分页查询用户的信息
     */
    @Override
    public PagedResult<CustomerView> queryPagedCustomerView(String key, String sort, String order, Integer pageNo,
															Integer pageSize) {
        pageNo = pageNo == null ? 0 : pageNo;
        pageSize = pageSize == null ? 10 : pageSize;
        PageHelper.startPage(pageNo, pageSize);
        return BeanUtil.topagedResult(daoFactory.getCustomerMapper().selectCustomer(key, sort, order));
    }

    /**
     * 查询用户的信息
     */
    @Override
    public List<CustomerView> queryAllCustomer() {
        return customerDTO.queryAllCustomer();
    }

    /**
     * 修改用户信息
     */
    @Transactional
    @Override
    public boolean modifyCustomer(CustomerView customerView) {
        return customerView.modifyCustomer();
    }

    /**
     * 查询权限
     */
    @Override
    public List<PermissionDTO> queryPermission() {
        return permissionDTO.queryPermission();
    }

    /**
     * 查询权限
     */
    @Override
    public List<UserSector> queryAllUser() {
        return daoFactory.getUserinfoMapper().selectAllUserByDep();
    }

    /**
     * 查询权限
     */
    @Override
    public List<CommentDTO> queryComment() {
        return commentDTO.queryComment();
    }

    @Override
    public boolean insertComment(CommentDTO comment) {
        return commentDTO.insertComment(comment);
    }

    /**
     * 修改权限的状态
     */
    @Override
    public boolean modifyPermission(PermissionDTO permission) {
        return permissionDTO.modifyPermission(permission);
    }

    /**
     * 查询所有的维修项目
     */
    @Override
    public List<MainItemView> queryAllMainItem() {
        return daoFactory.getMainitemMapper().selectallMainitem();
    }

    @Override
    public boolean modifyMainitem(MainitemDTO mainitem, String type) {
        return mainitemDTO.modifyMainitem(mainitem,type);
    }

    /**
     * 查询所有维修的类别
     */
    @Override
    public List<ProjcategoryDTO> queryAllProjCategory() {
        return daoFactory.getProjcategoryMapper().selectAllCategory();
    }

    /**
     * 修改维修项目大类
     */
    @Override
    public boolean modifyPartCategory(ProjcategoryDTO projcategory, String type) {
        return projcategoryDTO.modifyPartCategory(projcategory, type);
    }

    /**
     * 查询所有的供应商
     */
    @Override
    public PagedResult<SupplierDTO> queryAllSupplier(String key, String sort, String order, String flag, Integer pageNo,
                                                     Integer pageSize) {
        pageNo = pageNo == null ? 0 : pageNo;
        pageSize = pageSize == null ? 10 : pageSize;
        PageHelper.startPage(pageNo, pageSize);
        return BeanUtil.topagedResult(daoFactory.getSupplierMapper().selectAllSupplier(key, sort, order, flag));
    }


    /**
     * 修改供应商的信息
     */
    @Override
    public boolean modifySupplier(SupplierDTO supplier, String type) {

        return supplierDTO.modifySupplier(supplier,type);
    }

    /**
     * 查询所有的供应商
     */
    @Override
    public List<SupplierDTO> queryAllSupplier() {
        return supplierDTO.queryAllSupplier();
    }


    /**
     * 查询所有的配件的类别
     */
    @Override
    public PagedResult<PartcategoryDTO> queryPagedPartCategory(Integer pageNo, Integer pageSize, String status) {
        pageNo = pageNo == null ? 0 : pageNo;
        pageSize = pageSize == null ? 0 : pageSize;
        PageHelper.startPage(pageNo, pageSize);
        return BeanUtil.topagedResult(daoFactory.getPartcategoryMapper().selectAllPartCategory(status));
    }

    /**
     * 修改零件类别信息
     */
    @Override
    public boolean modifyPartCategory(PartcategoryDTO partcategory, String type) {

        return partcategoryDTO.modifyPartCategory(partcategory,type);
    }

    /**
     * 查询所有的仓库的信息
     */
    @Override
    public PagedResult<WarehouseDTO> queryPagedWarehouse(Integer pageNo, Integer pageSize, String status) {
        pageNo = pageNo == null ? 0 : pageNo;
        pageSize = pageSize == null ? 0 : pageSize;
        PageHelper.startPage(pageNo, pageSize);
        return BeanUtil.topagedResult(daoFactory.getWarehouseMapper().selectAllWarehouse(status));
    }

    /**
     * 修改仓库的信息
     */
    @Override
    public boolean modifyWarehouse(WarehouseDTO warehouse, String type) {

        return warehouseDTO.modifyWarehouse(warehouse,type);
    }

    /**
     * 查询所有零件的类别
     */
    @Override
    public List<PartcategoryDTO> queryAllPartCategory() {
        return daoFactory.getPartcategoryMapper().selectAllPartCategory("");
    }

    /**
     * 查询所有的仓库
     */
    @Override
    public List<WarehouseDTO> queryAllWareHouse() {
        return daoFactory.getWarehouseMapper().selectAllWarehouse("");
    }

    /**
     * 添加零件存储的信息
     */
    @Override
    public boolean addPartStorage(PartDTO part, PartstorageDTO partstorage) {
        boolean flag = false;
        if (part != null && partstorage != null) {
            try {
                // 添加零件的信息
                int partid = daoFactory.getPartMapper().insertSelective(part);
                // partstorage 设置partid
                partstorage.setPartid(part.getId());
                logger.info("======>零件存储返回的零件的编号为：" + part.getId());
                // 添加零件存储的信息
                daoFactory.getPartstorageMapper().insertSelective(partstorage);
                flag = true;
            } catch (Exception e) {
                logger.info("添加材料信息有误...");
            }
        }
        return flag;
    }

    /**
     * 删除零件
     */
    @Override
    public boolean delPart(String partid) {
        return partDTO.delPart(partid);
    }

}
